//
//  CustomTabBarView.m
//  NewProject
//
//  Created by 创维海通 on 16/9/7.
//  Copyright © 2016年 创维海通. All rights reserved.
//

#import "CustomTabBarView.h"
@interface CustomTabBarView()
{
    UIButton *_lastButton;  //最后一次选中的Btn
    
    NSArray *icon;
    NSArray *selectIcon;
    NSArray *txt;
}

@end

@implementation CustomTabBarView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        icon = [[NSArray alloc] initWithObjects:@"earnings" ,@"me", nil];
        selectIcon = [[NSArray alloc] initWithObjects:@"earnings_select" ,@"me_select" , nil];
        txt = [[NSArray alloc] initWithObjects:@"收益", @"我的",nil];
        [self initTabBar];
    }
    return self;
}

#pragma mark -init
#define btnWidth 50
#define btnHeight 30
#define btnDistance SCREEN_WIDTH/2
- (void)initTabBar
{
    for (int i = 0; i < 2; i++) {
        UIButton *customBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [customBtn setImage:[UIImage imageNamed:icon[i]] forState:UIControlStateNormal];
        [customBtn setImage:[UIImage imageNamed:selectIcon[i]] forState:UIControlStateSelected];
        customBtn.frame = CGRectMake(btnDistance/2 - 20 + i * btnDistance , 8, btnWidth, btnHeight);
        customBtn.tag =  100 + i;
        
        [customBtn addTarget:self action:@selector(tapAction:) forControlEvents:UIControlEventTouchUpInside];
        
        UILabel *customLabel = [[UILabel alloc] initWithFrame:CGRectMake(btnDistance/2 - 20 + i * btnDistance, CGRectGetMaxY(customBtn.frame), btnWidth, 20)];
        customLabel.text = txt[i];
        customLabel.textAlignment = NSTextAlignmentCenter;
        customLabel.font = [UIFont fontWithName:@"Arial" size:13.0f];
        customLabel.textColor = HTCFQuickRGBColor(51, 51, 51);
        customLabel.tag = 200 + i;
        if (customBtn.tag == 100) {
            customBtn.selected = YES;
            _lastButton = customBtn;
            customLabel.textColor = HTCFQuickRGBColor(250, 50, 2);
        }
        
        [self addSubview:customBtn];
        [self addSubview:customLabel];
    }
}

#pragma mark click
- (void)tapAction:(UIButton *)btn
{
    if (_lastButton != btn) {
        for (UILabel *label in self.subviews) {
            if ([label isKindOfClass:NSClassFromString(@"UILabel")]) {
                label.textColor = HTCFQuickRGBColor(51, 51, 51);
            }
        }
        
        _lastButton.selected = NO;
        _lastButton = btn;
        _lastButton.selected = YES;
        UILabel *label = [self viewWithTag:100 + btn.tag];
        label.textColor = HTCFQuickRGBColor(250, 50, 2);
        if ([_tabBardelegate respondsToSelector:@selector(selectController:)]) {
            [_tabBardelegate selectController:(int)btn.tag];
        }
        
        [self playTransitionAnimation:btn];
    }
}

- (void)playTransitionAnimation:(UIButton *)iconBtn
{
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.y"];
    
    animation.values = @[@(0), @(M_PI_2), @(M_PI), @(M_PI * 2)];
    animation.duration = 0.4;
    animation.calculationMode = kCAAnimationCubic;
    
    [iconBtn.layer addAnimation:animation forKey:@"playTransitionAnimation"];
}

@end
